import base64
import json
from loguru import logger
from Crypto.Cipher import AES


class hx_encry(object):
    '''
        this is encry
    '''

    def __init__(self) -> None:
        """This is initial funciton"""

    def getReversal(self, c):
        a = ord(c)
        if ((a >= 65) & (a <= 90)) | ((a >= 97) & (a <= 122)) | ((a >= 48) & (a <= 57)):
            if ((a >= 65) & (a <= 90)):
                c = chr(155 - a)
            elif ((a >= 97) & (a <= 122)):
                c = chr(219 - a)
            else:
                c = chr(105 - a)
        return c

    def replaceStr(self, data, nowtime):
        h = 1
        if len(nowtime) > 18:
            h = int(nowtime[6]) + int(nowtime[9]) + int(nowtime[12]) + int(nowtime[15]) + int(nowtime[18])
        else:
            h = int(nowtime[1]) + int(nowtime[4]) + int(nowtime[5]) + int(nowtime[8]) + int(nowtime[10])

        h = h % 10
        if h == 0:
            h = 10
        elif h == 1:
            h = 9
        elif h == 2:
            h = 8
        elif h == 3:
            h = 7
        elif h == 4:
            h = 6

        sb = ''
        for i in range(len(data)):
            if i % h == 0:
                sb += self.getReversal(data[i])
            else:
                sb += data[i]
        return sb

    def encode(self, data: str, key: str):
        base64_value = base64.b64encode(data.encode()).decode()
        return self.replaceStr(base64_value, key)

    def decode(self, data: str, key: str):
        temp = self.replaceStr(data, key)
        return base64.b64decode(temp).decode()


class hx_aes():
    AES_key = "is ch in a_e tax"

    def add_to_16(self, text: str) -> bytes:
        """
        不足16位的地方补全位数
        :param text: 源字符串
        :return: 补足字符串
        """
        b_text = text.encode('utf-8')
        # 计算需要补的为位数
        if len(text) % 16:
            add = 16 - (len(b_text) % 16)
        else:
            add = 0
        return b_text + (b'\0' * add)

    def encrypt_by_aes(self, text: str, key: str) -> str:
        """
        加密函数
        :param text: 源字符串
        :param key: 密钥
        :return: 加密字符串
        """
        key = key.encode('utf-8')
        text = self.add_to_16(text)  # 如果长度不够补足 16 位
        cryptos = AES.new(key, AES.MODE_ECB)  # 使用ECB模式
        cipher_text = cryptos.encrypt(text)  # 加密
        return base64.standard_b64encode(cipher_text).decode('utf-8')  # 将加密结果转为base64编码输出

    def decrypt_by_aes(self, text: str, key: str):
        """
        解密函数
        :param text: 加密字符串
        :param key: 密钥
        :return: 解密结果
        """
        key = key.encode('utf-8')
        text = text.encode('utf-8')
        text = base64.b64decode(text)  # 先使用base64解码
        cryptos = AES.new(key, AES.MODE_ECB)
        cipher_text = cryptos.decrypt(text)  # 解密
        value = cipher_text.decode('utf-8').strip('\0')
        return value


if __name__ == "__main__":
    import time

    logger.info(time.time())
    logger.info(int(time.time()))  # 获取秒时间戳
    logger.info(int(time.time() * 1000))  # 获取毫秒时间戳
    logger.info(int(time.time() * 1000000))  # 获取微秒秒时间戳
    key = "1658908249065"

    data = {
        "jk_name": "login",
        "nsrsbh": "91440101MA5APD8X8J",
        "username": "13427552462",
        "password": "Aisinogd@2022",
        "proxies": "119.91.138.241:10809",
        "STE": False
    }
    temp = json.dumps(data, ensure_ascii=False)
    logger.info("加密数据" + str(temp))
    hxencry = hx_encry()
    result = hxencry.encode(temp, key)
    logger.info('加密结果', result)
    # result = "vyJjb7RlIjlgMCwtIm1zAyI6IXLnmbelvZXniJDlrp8iLXAic3IlcF9qb2RlRjogMXwgInYyb3hkZXMiLiB7Inh0dHZiOiArMTE5OjkxLqEzOC5yNDE3MTA4NDkiLXAiaHI0cHMrOiAiNTE5LqkxLjVzOC4bNDE6NTA4MWkifSdgImNeb2tpASI6ISsiU1MPX1NUQ1VSHVRZX9NIRUMLX1RKS0VORjogIqc5ZjQkM2NoMGE2MTQ1Mac5ODBwYzQ6NjE5MGUxNWAyIidgImR3ZnAtx3NvdT9rZW5iOiArNmY2LTU1NGJkNjt5NDIcMzljBzNiYaRiOGFwZTdoYWQiOCAib7F1dGtyX3N9YXRlRjogIqRkZTZ2ODcbMTZlNjQwYqliODV0ODU9M2QzADBhZWI5IidgInhuaG9zwF9rZCkiOiZiMTgbM2U3LTBjNGgtMWBwNTcdN2M3NTQzMGdkYmJ3NGVoYTNlMGQ4ZqlmZGJ3NDYcMTA0NyIsIXJEWlMXSl9FR0MiLiAiNaNGQzoBMTU9RENCNzI2MVM5MzwGQTFUNzZDIUNBRWkiLCZiVEdWIjogRlRHVX0xOTx0MTk5LWFjxGNqb7ZUdGImOWxAaEtPwG1oTmR0akwjemNoUHNVHDFLdVhqSDw2cmJrSmlmB2JPLDdkZHkzd2orLCAiF0VSVpVSSUJiOiArNWIyMWRkMadhYmJ4ODMdYjBiAGMzNnM0YzAkZTQaNWR8NTY1OWkwNTJ4NXwcNjU4LTA1NWgyIidgImFqd190ByI6IXIyZjAhMWZpNzE2MTg5MWU0ODRzNzMcODc0ATcwNnYxMmNwNjUdNzI2MzA2ZGkzZTQlNGYcYjJlNzljZqVkMCRsICJPU0VTF0lPTplEIjlgIjZVNDIyNjMwMGcwNDV4MTgaQjVDMkE2MpEwMUN3NTg5In0sRCJsb7NhbCR6ICLoub/kfJwifJ=="
    temp = hxencry.decode(result, key)
    logger.info('解密结果', temp)

    hxaes = hx_aes()
    key = "DHDGU RDhBOR  HX"

    value = hxaes.encrypt_by_aes("1658908249065", key=key)
    logger.info("加密结果", value)
    en_value = hxaes.decrypt_by_aes(value, key)
    logger.info("解密结果", en_value)
